/**
 * @author        Nicolas Bottarini <nicolasbottarini@gmail.com>, 404 <http://www.proyecto404.com>
 * @version       $Rev: 3 $
 * @lastrevision  $Date: 2011-09-12 16:45:37 +0000 (Mon, 12 Sep 2011) $	 
 */
package com.proyecto404.core.mvc {
	import com.proyecto404.core.Check;
	import com.proyecto404.core.EventArgs;
	import com.proyecto404.core.collections.Dictionary;
	import com.proyecto404.core.ui.util.DisplayObjectHelper;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	
	import org.osflash.signals.ISignal;
	import org.osflash.signals.Signal;

	public class LayoutView extends View {
		private var _viewChanged:Signal;
		private var _registeredHolders:Dictionary;
		private var _holderViews:Dictionary;
		public var mainHolder:MovieClip;
		
		
		public function LayoutView() {
			super();
			_viewChanged = new Signal(LayoutViewChangedEventArgs);
			_registeredHolders = new Dictionary();
			_holderViews = new Dictionary();
			addEventListener(Event.ADDED, initializeMainHolder);
		}
		
		public function get viewChanged():ISignal {
			return _viewChanged;	
		}
		
		protected function registerHolder(holderName:String,holderMc:MovieClip):void {
			_registeredHolders.add(holderName, holderMc);
			_holderViews.add(holderName, null);
			DisplayObjectHelper.removeAllChildren(holderMc);
		}
		
		protected function hasHolder(holderName:String):Boolean {
			return _registeredHolders.containsKey(holderName);
		}
		
		protected function getHolder(holderName:String):MovieClip {
			return _registeredHolders.get(holderName) as MovieClip;
		}

		private function initializeMainHolder(e:Event):void {
			if (mainHolder != null) {
				registerHolder("mainHolder", mainHolder);
				removeEventListener(Event.ADDED, initializeMainHolder);									
			}
		}
		
		public function get currentView():View {
			return getHolderView("mainHolder");
		}
		
		public function getHolderView(holderName:String = "mainHolder"):View {
			return _holderViews.get(holderName);
		}

		public function setHolderView(holderName:String, view:View):void {
			return _holderViews.add(holderName, view);
		}

		public function setView(view:View, holderName:String = "mainHolder"):void {
			Check.require(hasHolder(holderName), "Placeholder required with instance name " + holderName);
			if (getHolderView(holderName) != null && getHolderView(holderName).isOpened()) {
				getHolderView(holderName).closed.addOnce(function(e:EventArgs):void {
					doSetView(view, holderName);
				});
				getHolderView(holderName).close();
			} else {
				doSetView(view, holderName);
			}
		}
		
		private function doSetView(view:View, holderName:String):void {
			if (getHolderView(holderName) != null) {
				getHolder(holderName).removeChild(getHolderView(holderName));
			}
			setHolderView(holderName, view);
			getHolder(holderName).addChild(view);
			_viewChanged.dispatch(new LayoutViewChangedEventArgs(holderName));
			view.open();
		}
		
		public override function close():void {
			// TODO: Cerrar todas las vistas de los holders
			if (getHolderView("mainHolder") != null) {
				getHolderView("mainHolder").closed.addOnce(function(e:EventArgs):void {
					closeLayout();
				});
				getHolderView("mainHolder").close();
			} else {
				closeLayout();
			}
		}
		
		private function closeLayout():void {
			super.close();
		}
	}
}